package com.lyn.frp.config;

import android.text.TextUtils;

import androidx.annotation.NonNull;

/**
 * Frp配置实体
 *
 * @author Lyn 2022/12/26
 * @version 1.0.0
 */
public class FrpConfig {

    public String uid;
    public String common;
    public String serverAddress;
    public int serverPort;
    public String token;
    public String ssh;
    public String type;
    public String localIp;
    public int localPort;
    public int remotePort;
    public String plugin;
    public String pluginLocalPath;
    public String pluginStripPrefix;
    public String pluginHttpUser;
    public String pluginHttpPasswd;

    public FrpConfig(Builder builder) {
        this.uid = builder.uid;
        this.common = builder.common;
        this.serverAddress = builder.serverAddress;
        this.serverPort = builder.serverPort;
        this.token = builder.token;
        this.ssh = builder.ssh;
        this.type = builder.type;
        this.localIp = builder.localIp;
        this.localPort = builder.localPort;
        this.remotePort = builder.remotePort;
        this.plugin = builder.plugin;
        this.pluginLocalPath = builder.pluginLocalPath;
        this.pluginStripPrefix = builder.pluginStripPrefix;
        this.pluginHttpUser = builder.pluginHttpUser;
        this.pluginHttpPasswd = builder.pluginHttpPasswd;
    }

    public static class Builder {

        private String uid;
        private String common = "[common]";
        private String serverAddress;
        private int serverPort;
        private String token;
        private String ssh = "[ssh]";
        private String type;
        private String localIp;
        private int localPort;
        private int remotePort;
        private String plugin;
        private String pluginLocalPath;
        private String pluginStripPrefix;
        private String pluginHttpUser;
        private String pluginHttpPasswd;

        public Builder setUid(String uid) {
            this.uid = uid;
            return this;
        }

        public Builder setCommon(String common) {
            this.common = common;
            return this;
        }

        public Builder setServerAddress(String serverAddress) {
            this.serverAddress = serverAddress;
            return this;
        }

        public Builder setServerPort(int serverPort) {
            this.serverPort = serverPort;
            return this;
        }

        public Builder setToken(String token) {
            this.token = token;
            return this;
        }

        public Builder setSsh(String ssh) {
            this.ssh = ssh;
            return this;
        }

        public Builder setType(String type) {
            this.type = type;
            return this;
        }

        public Builder setLocalIp(String localIp) {
            this.localIp = localIp;
            return this;
        }

        public Builder setLocalPort(int localPort) {
            this.localPort = localPort;
            return this;
        }

        public Builder setRemotePort(int remotePort) {
            this.remotePort = remotePort;
            return this;
        }

        public Builder setPlugin(String plugin) {
            this.plugin = plugin;
            return this;
        }

        public Builder setPluginLocalPath(String pluginLocalPath) {
            this.pluginLocalPath = pluginLocalPath;
            return this;
        }

        public Builder setPluginStripPrefix(String pluginStripPrefix) {
            this.pluginStripPrefix = pluginStripPrefix;
            return this;
        }

        public Builder setPluginHttpUser(String pluginHttpUser) {
            this.pluginHttpUser = pluginHttpUser;
            return this;
        }

        public Builder setPluginHttpPasswd(String pluginHttpPasswd) {
            this.pluginHttpPasswd = pluginHttpPasswd;
            return this;
        }

        public FrpConfig create() {
            return new FrpConfig(this);
        }
    }

    @NonNull
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.common).append("\r\n");
        if (!TextUtils.isEmpty(this.serverAddress)) {
            sb.append("server_addr = ").append(this.serverAddress).append("\r\n");
        }
        sb.append("server_port = ").append(this.serverPort).append("\r\n");
        if (!TextUtils.isEmpty(this.token)) {
            sb.append("token = ").append(this.token).append("\r\n");
        }
        sb.append("\r\n");
        if (!TextUtils.isEmpty(this.ssh)) {
            sb.append(this.ssh).append("\r\n");
        }
        if (!TextUtils.isEmpty(this.type)) {
            sb.append("type = ").append(this.type).append("\r\n");
        }
        if (!TextUtils.isEmpty(this.localIp)) {
            sb.append("local_ip = ").append(this.localIp).append("\r\n");
        }
        if (this.localPort != 0) {
            sb.append("local_port = ").append(this.localPort).append("\r\n");
        }
        if (this.remotePort!=0) {
            sb.append("remote_port = ").append(this.remotePort).append("\r\n");
        }
        if (!TextUtils.isEmpty(this.plugin)) {
            sb.append("plugin = ").append(this.plugin).append("\r\n");
        }
        if (!TextUtils.isEmpty(this.pluginLocalPath)) {
            sb.append("plugin_local_path = ").append(this.pluginLocalPath).append("\r\n");
        }
        if (!TextUtils.isEmpty(this.pluginStripPrefix)) {
            sb.append("plugin_strip_prefix = ").append(this.pluginStripPrefix).append("\r\n");
        }
        if (!TextUtils.isEmpty(this.pluginHttpUser)) {
            sb.append("plugin_http_user = ").append(this.pluginHttpUser).append("\r\n");
        }
        if (!TextUtils.isEmpty(this.pluginHttpPasswd)) {
            sb.append("plugin_http_passwd = ").append(this.pluginHttpPasswd).append("\r\n");
        }
        sb.append("\r\n");
        return sb.toString();
    }
}
